package pers.gs.Dao.Impl;

/**
 * StudentDao.java
 */

import pers.gs.Dao.IStudentDao;
import pers.gs.Entity.Student;
import pers.gs.util.DBUtil;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据访问层
 */
public class StudentDaoImpl implements IStudentDao {

    /*
     *根据学号，修改学生信息
     */
    public boolean updateStudentBySno(String sno, Student student){
        String sql = "update student set name=?,age=?,gender=? where sno = ?";
        Object[] objects = {student.getName(),student.getAge(),student.getGender(),student.getSno()};
        return DBUtil.executeUpdate(sql ,objects);
    }

    /*
     *根据学号，删除学生信息
     */
    public boolean deleteStudentBySno(String sno){
        String sql = "delete from student where sno = ?";
        Object[] objects = {sno};
        return DBUtil.executeUpdate(sql ,objects);
    }

    /*
     * 新增学生信息
     */
    public boolean addStudent(Student student){
        String sql = "insert into student values(?,?,?,?)";
        Object[] objects = {student.getSno(),student.getName(),student.getAge(),student.getGender()};
        return DBUtil.executeUpdate(sql ,objects);
    }


    /*
     * 根据学号，查询学生
     */
    public Student queryStudentBySno(String usno) {
        String sql = "select * from student where sno = ?";
        Object[] objects = {usno};
        ResultSet rs = null;
        Student student = null;
        try{
            //1~3. 执行通用查询
            rs = DBUtil.executeQuery(sql, objects);
            //4.处理结果
            if (rs.next()) {//如果有数据
                String sno = rs.getString("sno");
                String name = rs.getString("name");
                int age = rs.getInt("age");
                String gender = rs.getString("gender");
                student = new Student(sno,name,age,gender);
            }
            return student;
        }catch (SQLException e) {
            e.printStackTrace();
            return null;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                DBUtil.closeAll(rs,DBUtil.pstmt,DBUtil.conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /*
     *查询全部学生
     */
    public List<Student> queryAllStudent(){
        List<Student> studentList = new ArrayList<>();
        String sql = "select * from student";
        ResultSet rs = null;
        Student student;
        try{
            //1~3.执行通用查询操作
            rs = DBUtil.executeQuery(sql,null);

            //4.处理结果
            while (rs.next()) {//如果有数据
                String sno = rs.getString("sno");
                String name = rs.getString("name");
                int age = rs.getInt("age");
                String gender = rs.getString("gender");
                student = new Student(sno,name,age,gender);
                studentList.add(student);
            }
            return studentList;
        }catch (SQLException e) {
            e.printStackTrace();
            return null;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            try {
                DBUtil.closeAll(rs,DBUtil.pstmt,DBUtil.conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /*
     *是否存在，根据学号判断该学生是否存在
     * 如果学生信息存在，返回true
     * 不存在，返回false
     */
    public boolean isExist(String sno){
        return queryStudentBySno(sno) == null ? false:true;
    }
}
